<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>

<script>
    var search_type_student = "none";
    var selectID,action = null,fileName;

    $(function() {
        init();
        datePickerInit();
        searchAction();
        dataListInit();
        bootstrapValidatorInit();

        submitAction();
        deleteAction();
        deleteBatchAction();
    })

    function init(){

        if(store.get('userRole')=="2"){
            $("#add").hide();
            $("#edit").hide();
            $("#batch_delete").hide();
            $("#del").hide();
        }else{
            $("#add").show();
            $("#edit").show();
            $("#batch_delete").show();
            $("#del").show();
            searchCourse("#lessonNameSearch");

        }
    }

    function datePickerInit(){

        $('.form_date').datetimepicker({
            format:'yyyy-mm-dd',
            language : 'zh-CN',
            weekStart : 1,
            todayBtn : 1,
            autoClose : 1,
            todayHighlight : 1,
            startView : 2,
            forceParse : 0,
            minView:2
        });
    }

    //搜索课程列表
    function searchCourse(subject){

        $.ajax({
            url:"/onlineEdu/lesson/getLessonGroup",
            type: 'GET',
            cache: false,
            data:{},
            dataType:  "json",
            async:false,
            success: function (data) {
				var options = '<option value="">--请选择--</option>'+data.lessonGroup;
                $(subject).html(options);
                $("#lessonName").html(options);
            }
        });
    }

    // 搜索动作
    function searchAction() {
        $('#search_button').click(function() {
            search_keyWord = $('#search_input').val();
            tableRefresh();
        })
    }

    // 分页查询参数
    function queryParams(params) {

        var temp = {
            limit : params.limit,
            offset : params.offset,
            lessonName :$('#lessonNameSearch').val(),
            lessonClass:$('#classSearch').val(),
            teacherName:$('#teacherSearch').val(),
            subject:$('#subjectSearch').val(),
            startTime:$('#startTimeSearch').val(),
            endTime:$('#endTimeSearch').val()
        }
        return temp;
    }

    // 表格初始化
    function dataListInit() {
        $('#dataList').bootstrapTable(
            {
                columns : [{
                    checkbox: true,
                    align: 'center'
                }, {
                    title: 'ID',
                    formatter: function (value, row, index) {
                        return index+1;
                    },
                    align: "center"
                },
                    {
                        field : 'lesson_name',
                        title : '课程名称'
                    },
					{
                        field : 'lesson_class',
                        title : '年级'
                    },
                    {
                        field : 'teacher_name',
                        title : '上课教师'
                    },
                    {
                        field : 'date_time',
                        title : '上课日期'
                    },
                    {
                        field : 'subject',
                        title : '课程主题'
                    },
                    {
                        field : 'video_url',
                        title : '视频ID'
                    },
                    {
                        field : 'operation',
                        title : '操作',
                        width: 140,
                        align: 'center',
                        valign: 'middle',
                        formatter : function(value, row, index) {
                            var s = '<button id="edit" class="btn btn-info btn-sm edit"><span>编辑</span></button>';
                            var d = '<button id="del" class="btn btn-danger btn-sm delete"><span>删除</span></button>';
                            return s + ' '+ d;
                        },
                        events : {
                            // 操作列中编辑按钮的动作
                            'click .view' : function(e, value, row, index) {
                                selectID = row.lesson_record_id;
                                rowEditOperation(row,'view');
                            },
                            'click .edit' : function(e, value, row, index) {
                                selectID = row.lesson_record_id;
                                fileName = row.video_url;
                                if (store.get('userRole') == "0") {
                                    rowEditOperation(row, 'edit');
                                }else{
                                    alert("无权限");
                                }
                            },
                            'click .delete' : function(e,value, row, index) {
                                selectID = row.lesson_record_id;
                                if (store.get('userRole') == "0") {
                                    $('#deleteWarning_modal').modal(
                                        'show');
                                }else{
                                    alert("无权限");
								}
                               // deleteAction();
                            }
                        }
                    } ],
                url : '/onlineEdu/lessonRecord/getLessonRecordByPage',
                onLoadError:function(status){
                    handleAjaxError(status);
                },
                method : 'GET',
                queryParams : queryParams,
                sidePagination : "server",
                dataType : 'json',
                pagination : true,
                pageNumber : 1,
                pageSize: 10,
                pageList : [5, 10, 25, 50, 100 ],
                clickToSelect : true
            });
    }

    // 表格刷新
    function tableRefresh() {
        $('#dataList').bootstrapTable('refresh', {
            query : {}
        });
    }
    // 行编辑操作
    function rowEditOperation(row,model) {

		$('#add_modal').modal("show");
        $('#titleLable').html("修改数据");
        $('#modal_cancel').show();
        $('#modal_submit').show();
        action="edit";
        $('#lessonName').val(row.lesson_name);
        getTeacher();
        $('#lessonClass').val(row.lesson_class);
        $('#teacherName').val(row.teacher_name);
        $('#subject').val(row.subject);
        $('#dateTime').val(row.date_time);
        $('#videoId').val(row.video_url);
        $('#pictureUrl').val(row.picture_url);
    }

    // 刪除信息
    function deleteAction(){

        $('#delete_confirm').click(function(){
            var ids = '';
            var rows = $("#dataList").bootstrapTable('getSelections');
            for (var i = 0; i < rows.length; i++) {
                ids += "'"+rows[i]['lesson_record_id'] +"'"+ ",";
            }
            ids = ids.substring(0, ids.length - 1);
            var data = {
                "lessonRecordId" : ids
            }
            deleteData(data);
        })
    }

    function deleteBatchAction() {
        //获取所有被选中的记录
        $('#batch_delete').click(function() {
            var rows = $("#dataList").bootstrapTable('getSelections');
            if (rows.length== 0) {
                alert("请先选择要删除的记录!");
            }else {
                $('#deleteWarning_modal').modal('show');
               // deleteAction();
            }
        });
    }

    function deleteData(data) {

        $.ajax({
            type : "GET",
            url : "/onlineEdu/lessonRecord/delete",
            dataType : "json",
            contentType : "application/json",
            data : data,
            success : function(response){
                $('#deleteWarning_modal').modal("hide");
                var type;
                var msg;
                var append = '';
                if(response.success){
                    type = "success";
                    msg = "删除成功";
                }else{
                    type = "error";
                    msg = "删除失败";
                }
                showMsg(type, msg, append);
                tableRefresh();
            },
            error : function(response){
                $('#deleteWarning_modal').modal("hide");
                // handle error
                alert("失败");
            }
        })
        $('#deleteWarning_modal').modal('hide');
    }
    // 添加信息
    function submitAction() {
        $('#add').click(function() {
            $('#add_modal').modal("show");
            $('#titleLable').html("添加数据");
            $('#modal_cancel').show();
            $('#modal_submit').show();
            action = 'add';
            $('#lessonName').val("");
            $('#lessonClass').val("");
            $('#teacherName').val("");
            $('#subject').val("");
            $('#dateTime').val("");
            $('#videoId').val("");
            $('#pictureUrl').val("");
        });

        $('#modal_submit').click(function() {
            var data = {
                lessonName:$('#lessonName').val(),
                lessonClass:$('#lessonClass').val(),
                teacherName:$('#teacherName').val(),
                subject:$('#subject').val(),
                dateTime:$('#dateTime').val(),
                videoUrl:$('#videoId').val(),
                pictureUrl:$('#pictureUrl').val()
            }
            if(!(data.lessonName&&data.lessonClass&&data.teacherName&&
                    data.subject&&data.dateTime&&data.pictureUrl)) {
                alert("请填写完整！");
                return ;
            }
            if(!(data.videoUrl)) {
                alert("请上传课程录像！");
                return ;
            }
            var url;
            if(action =='edit'){
                url = "/onlineEdu/lessonRecord/update";
                data.lessonRecordId = selectID;
            }else{
                url = "/onlineEdu/lessonRecord/save";
            }
            $.ajax({
                type : "POST",
                url :url,
                dataType : "json",
                data : data,
                success : function(response) {
                    $('#add_modal').modal("hide");
                    var msg;
                    var type;
                    var append = '';
                    if (response.success) {
                        type = "success";
                        msg = "操作成功";
                    } else {
                        type = "error";
                        msg = "失败";
                    }
                    tableRefresh();
                    showMsg(type, msg, append);
                    $('#lessonName').val("");
                    $('#lessonClass').val("");
                    $('#teacherName').val("");
                    $('#subject').val("");
                    $('#dateTime').val("");
                    $('#submitFile').val("");
                    $('#videoId').val("");
                    $('#pictureUrl').val("");
                },
                error : function(xhr, textStatus, errorThrown) {
                    $('#add_modal').modal("hide");
                    handleAjaxError(xhr.status);
                }
            })
        })
    }

    function getTeacher(){
        $.ajax({
            type : "GET",
            url :"/onlineEdu/teacher/getTeacherByLesson",
            dataType : "json",
            contentType : "application/json",
            data : {lesson:$("#lessonName").val()},
            cache: false,
            async:false,
            success : function(response) {
                $("#teacherName").empty();
                var options="",teachers = response.teachers;
                teachers.forEach(function (item, index){
                    options += '"<option value=\"'+item.teacher_name+'\" selected>'+item.teacher_name+'</option>"';
                });
                options+='"<option value="" selected>'+"--请选择--"+'</option>"';
                $("#teacherName").html(options);
            },
            error : function(xhr, textStatus, errorThrown) {
                handleAjaxError(xhr.status);
            }
        })
    }
</script>
<div class="panel panel-default">
	<ol class="breadcrumb">
		<li><label class="form-label">上课记录</label></li>
	</ol>
	<div class="panel-body">
		<div class="row">
			<div style="width: 100%;margin-top: -16px" class="box-content">
				<table id="信息查询" class="table table-bordered table-hover table-responsive">
					<tbody>
					<tr>
						<td class="item-title" align="center">课程名称</td>
						<td class="item-content-half">
							<select id="lessonNameSearch" class="form-control">
							</select>
						</td>
						<td class="item-title" align="center">年级</td>
						<td class="item-content-half">
							<select id="classSearch" class="form-control">
								<option value="">--请选择--</option>
								<option value="小班">小班</option>
								<option value="小升中">小升中</option>
								<option value="中班">中班</option>
								<option value="中升大">中升大</option>
								<option value="大班">大班</option>
								<option value="大升一">大升一</option>
								<option value="一年级">一年级</option>
								<option value="一升二">一升二</option>
								<option value="二年级">二年级</option>
								<option value="二升三">二升三</option>
								<option value="三年级">三年级</option>
								<option value="三升四">三升四</option>
								<option value="四年级">四年级</option>
								<option value="四升五">四升五</option>
								<option value="五年级">五年级</option>
								<option value="小升初">小升初</option>
							</select>
						</td>
						<td class="item-title" align="center">上课教师</td>
						<td class="item-content-half">
							<input type="text" class="form-control" id="teacherSearch" placeholder="">
						</td>

					</tr>
					<tr>
						<td class="item-title" align="center">课程主题</td>
						<td class="item-content-half">
							<input type="text" class="form-control" id="subjectSearch" placeholder="">
						</td>
						<td class="item-title" align="center">上课开始日期</td>
						<td class="item-content-half" style="text-align: left">
							<input type="text" class="form_date form-control" id="startTimeSearch" placeholder="">
						</td>
						<td class="item-title" align="center">上课结束日期</td>
						<td class="item-content-half" style="text-align: left">
							<input type="text" class="form_date form-control" id="endTimeSearch" placeholder="">
						</td>
					</tr>
					</tbody>
				</table>
			</div>

			<%--</div>--%>
		</div>
		<div style="text-align: right">
			<button id="search_button" style="text-align: right" class="btn btn-success">
				<span class="glyphicon glyphicon-search"></span> <span>查询</span>
			</button>
		</div>
	</div>

	<div class="row" style="margin-top: 25px">
		<div class="col-md-5">
			<button class="btn btn-sm btn-default" id="add">
				<span class="glyphicon glyphicon-plus"></span> <span>添加</span>
			</button>
			<button class="btn btn-sm btn-default" id="batch_delete">
				<span class="glyphicon glyphicon-export"></span> <span>批量删除</span>
			</button>
		</div>
		<div class="col-md-5"></div>
	</div>

	<div class="row" style="margin-top: 15px">
		<div class="col-md-12">
			<table id="dataList" class="table table-striped"></table>
		</div>
	</div>
</div>
</div>

<!-- 添加信息模态框 -->
<div id="add_modal" class="modal fade" table-index="-1" role="dialog"
	 aria-labelledby="myModalLabel" aria-hidden="true"
	 data-backdrop="static">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button class="close" type="button" data-dismiss="modal"
						aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="titleLable">添加信息</h4>
			</div>
			<div class="modal-body">
				<!-- 模态框的内容 -->
				<div class="row">
					<div class="col-md-1 col-sm-1"></div>
					<div class="col-md-8 col-sm-8">
						<form class="form-horizontal" role="form" id="student_form"
							  style="margin-top: 25px">
							<div class="form-group">
								<label  class="control-label col-md-4 col-sm-4"> <span>课程名称：</span>
								</label>
								<div class="col-md-8 col-sm-8">
									<select id="lessonName" onblur="getTeacher()" class="form-control">
									</select>
								</div>
							</div>
							<div class="form-group">
								<label  class="control-label col-md-4 col-sm-4"> <span>年级：</span>
								</label>
								<div class="col-md-8 col-sm-8">
									<select id="lessonClass" class="form-control">
										<option value="">--请选择--</option>
										<option value="小班">小班</option>
										<option value="小升中">小升中</option>
										<option value="中班">中班</option>
										<option value="中升大">中升大</option>
										<option value="大班">大班</option>
										<option value="大升一">大升一</option>
										<option value="一年级">一年级</option>
										<option value="一升二">一升二</option>
										<option value="二年级">二年级</option>
										<option value="二升三">二升三</option>
										<option value="三年级">三年级</option>
										<option value="三升四">三升四</option>
										<option value="四年级">四年级</option>
										<option value="四升五">四升五</option>
										<option value="五年级">五年级</option>
										<option value="小升初">小升初</option>
									</select>
								</div>
							</div>
							<div class="form-group">
								<label  class="control-label col-md-4 col-sm-4"> <span>课程主题：</span>
								</label>
								<div class="col-md-8 col-sm-8">
									<input type="text" class="form-control" id="subject"
										   placeholder="课程主题">
								</div>
							</div>
							<div class="form-group">
								<label  class="control-label col-md-4 col-sm-4"> <span>上课时间：</span>
								</label>
								<div class="col-md-8 col-sm-8">
									<input type="text" class="form_date form-control" id="dateTime"
										   placeholder="上课时间">
								</div>
							</div>
							<div class="form-group">
								<label  class="control-label col-md-4 col-sm-4"> <span>上课教师：</span>
								</label>
								<div class="col-md-8 col-sm-8">
									<select id="teacherName" class="form-control">
									</select>
								</div>
							</div>
							<div class="form-group">
								<label  class="control-label col-md-4 col-sm-4"> <span>视频ID：</span>
								</label>
								<div class="col-md-8 col-sm-8">
									<input type="text" class="form-control" id="videoId"
										   placeholder="视频ID">
								</div>
							</div>
							<div class="form-group">
								<label  class="control-label col-md-4 col-sm-4"> <span>视频图片地址：</span>
								</label>
								<div class="col-md-8 col-sm-8">
									<input type="text" class="form-control" id="pictureUrl"
										   placeholder="视频图片地址">
								</div>
							</div>
						</form>
					</div>
					<div class="col-md-1 col-sm-1"></div>
				</div>
			</div>
			<div class="modal-footer">
				<button class="btn btn-default" type="button" data-dismiss="modal" id="modal_cancel">
					<span>取消</span>
				</button>
				<button class="btn btn-success" type="button" id="modal_submit">
					<span>提交</span>
				</button>
			</div>
		</div>
	</div>
</div>

<!-- 删除提示模态框 -->
<div class="modal fade" id="deleteWarning_modal" table-index="-1"
	 role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button class="close" type="button" data-dismiss="modal"
						aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="myModalLabelee">警告</h4>
			</div>
			<div class="modal-body">
				<div class="row">
					<div class="col-md-3 col-sm-3" style="text-align: center;">
						<img src="" alt=""
							 style="width: 70px; height: 70px; margin-top: 20px;">
					</div>
					<div class="col-md-8 col-sm-8">
						<h3>是否确认删除该条记录</h3>
						<p></p>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button class="btn btn-default" type="button" data-dismiss="modal">
					<span>取消</span>
				</button>
				<button class="btn btn-danger" type="button" id="delete_confirm">
					<span>确认删除</span>
				</button>
			</div>
		</div>
	</div>
</div>